package com.learner.ucenter.controller.api;

import com.alibaba.fastjson.JSON;
import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.common.util.JwtInfo;
import com.learner.common.util.JwtUtils;
import com.learner.pojo.user.Group;
import com.learner.pojo.user.vo.CreateGroupRequestVo;
import com.learner.pojo.user.vo.MyGroupResultVo;
import com.learner.pojo.user.vo.QuitGroupRequestVo;
import com.learner.pojo.user.vo.RecentGroupVo;
import com.learner.pojo.user.vo.SearchGroupResponseVo;
import com.learner.pojo.user.vo.SearchGroupResultVo;
import com.learner.pojo.user.vo.SearchRequestVo;
import com.learner.ucenter.service.GroupService;
import com.learner.ucenter.service.GroupUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ice2020x
 * @Date: 2023/2/27
 * @Description: 群聊控制器
 **/
@RestController
@RequestMapping("/api/ucenter/group")
@Slf4j
public class GroupController {

    @Autowired
    private GroupService groupService;

    @Autowired
    private GroupUserService groupUserService;


    /**
     * 获取我创建的群聊列表
     *
     * @return 分组列表信息
     */
    @GetMapping("/getMyCreateGroupList")
    public List<SearchGroupResultVo> getCreateGroupList(HttpServletRequest request) {
        String userId = JwtUtils.getCurrentUserId(request);
        log.info("获取我创建的群聊列表:{},用户id为:", userId);
        return groupService.getCreateGroupList(userId);
    }


    /**
     * 根据用户名获取我加入的群聊列表
     *
     * @param request request
     * @return 分组名称
     */
    @GetMapping("/getMyGroupList")
    public List<MyGroupResultVo> getMyGroupList(HttpServletRequest request) {
        String userId = JwtUtils.getCurrentUserId(request);
        List<MyGroupResultVo> myGroupList = groupUserService.getGroupUsersByUserName(userId);
        log.info("我的群聊列表为：{}", JSON.toJSONString(myGroupList));
        return myGroupList;
    }

    /**
     * 获取最近的群聊，根据id列表获取列表而已
     *
     * @param recentGroupVo 参数
     * @return result
     */
    @PostMapping("/recentGroup")
    public List<MyGroupResultVo> getRecentGroup(@RequestBody RecentGroupVo recentGroupVo) {
        log.info("最近的群聊列表请求参数为：" + recentGroupVo);
        List<MyGroupResultVo> recentGroups = groupUserService.getRecentGroup(recentGroupVo);
        log.info("最近的群聊列表为：" + recentGroups);
        return recentGroups;
    }


    /**
     * 获取群聊的详情
     *
     * @param groupId groupId
     * @return result
     */
    @GetMapping("/getGroupInfo")
    public Map<String, Object> getGroupInfo(String groupId) {
        Group groupInfo = groupService.getGroupInfo(groupId);
        log.info("查询出的群消息为：" + groupInfo);
        List<MyGroupResultVo> groupUsers = groupUserService.getGroupUsersByGroupId(groupId);
        log.info("群聊详情为：" + groupUsers);
        HashMap<String, Object> result = new HashMap<>(2);
        result.put("groupInfo", groupInfo);
        result.put("users", groupUsers);
        return result;
    }


    /**
     * 搜索群聊
     *
     * @param requestVo requestVo
     * @return 群聊列表
     */
    @PostMapping("/preFetchGroup")
    public List<SearchGroupResponseVo> searchGroup(@RequestBody SearchRequestVo requestVo, HttpServletRequest request) {
        JwtInfo infoByJwtToken = JwtUtils.getInfoByJwtToken(request);
        String userId = infoByJwtToken.getUserId();
        List<SearchGroupResponseVo> groupResponseVos = groupService.searchGroup(requestVo, userId);
        log.info("搜索的群聊:{}", JSON.toJSONString(groupResponseVos));
        return groupResponseVos;
    }

    /**
     * 创建群聊
     *
     * @param requestVo requestVo
     * @return 分组名称
     */
    @PostMapping("/createGroup")
    public String createGroup(@RequestBody CreateGroupRequestVo requestVo) {
        String groupCode = groupService.createGroup(requestVo);
        log.info("创建的群聊码：{}", groupCode);
        return groupCode;
    }

    /**
     * 获取群聊列表
     *
     * @return 分组列表信息
     */
    @GetMapping("/finAll")
    public List<SearchGroupResultVo> getAllGroup() {
        return groupService.getAllGroup();
    }


    /**
     * 退出群聊
     *
     * @param requestVo requestVo
     */
    @PostMapping("/quitGroup")
    public void quitGroup(@RequestBody QuitGroupRequestVo requestVo, HttpServletRequest request) {
        System.out.println("退出群聊的请求参数为：" + requestVo);
        JwtInfo infoByJwtToken = JwtUtils.getInfoByJwtToken(request);
        String userId = infoByJwtToken.getUserId();
        if (!userId.equals(requestVo.getUserId())) {
            //不是本人，非法操作
            throw ExceptionUtil.createException(RestException.class, 1000018);
        }
        groupService.quitGroup(requestVo);
    }
}